{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "to_train=pd.read_csv('input/train_7r07part.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>college</th>\n",
       "      <th>rank</th>\n",
       "      <th>total_people</th>\n",
       "      <th>rank_percent</th>\n",
       "      <th>countM1</th>\n",
       "      <th>price_sumM1</th>\n",
       "      <th>price_avgM1</th>\n",
       "      <th>price_maxM1</th>\n",
       "      <th>price_minM1</th>\n",
       "      <th>...</th>\n",
       "      <th>地点263_max</th>\n",
       "      <th>地点263_min</th>\n",
       "      <th>地点263_median</th>\n",
       "      <th>地点840_count</th>\n",
       "      <th>地点840_sum</th>\n",
       "      <th>地点840_avg</th>\n",
       "      <th>地点840_max</th>\n",
       "      <th>地点840_min</th>\n",
       "      <th>地点840_median</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>160</td>\n",
       "      <td>6.0</td>\n",
       "      <td>613.0</td>\n",
       "      <td>1570.0</td>\n",
       "      <td>0.390446</td>\n",
       "      <td>24.0</td>\n",
       "      <td>115.86</td>\n",
       "      <td>4.827500</td>\n",
       "      <td>12.3</td>\n",
       "      <td>0.43</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>27417</td>\n",
       "      <td>17.0</td>\n",
       "      <td>817.0</td>\n",
       "      <td>2051.0</td>\n",
       "      <td>0.398342</td>\n",
       "      <td>59.0</td>\n",
       "      <td>358.73</td>\n",
       "      <td>6.080169</td>\n",
       "      <td>23.8</td>\n",
       "      <td>0.01</td>\n",
       "      <td>...</td>\n",
       "      <td>5.6</td>\n",
       "      <td>3.6</td>\n",
       "      <td>4.6</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>14537</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1022.0</td>\n",
       "      <td>2775.0</td>\n",
       "      <td>0.368288</td>\n",
       "      <td>86.0</td>\n",
       "      <td>244.39</td>\n",
       "      <td>2.841744</td>\n",
       "      <td>10.6</td>\n",
       "      <td>0.01</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>119.0</td>\n",
       "      <td>4.25</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2396</td>\n",
       "      <td>9.0</td>\n",
       "      <td>2364.0</td>\n",
       "      <td>2933.0</td>\n",
       "      <td>0.806001</td>\n",
       "      <td>69.0</td>\n",
       "      <td>239.47</td>\n",
       "      <td>3.470580</td>\n",
       "      <td>21.4</td>\n",
       "      <td>-1.14</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>24354</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 432 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id  college    rank  total_people  rank_percent  countM1  price_sumM1  \\\n",
       "0    160      6.0   613.0        1570.0      0.390446     24.0       115.86   \n",
       "1  27417     17.0   817.0        2051.0      0.398342     59.0       358.73   \n",
       "2  14537      5.0  1022.0        2775.0      0.368288     86.0       244.39   \n",
       "3   2396      9.0  2364.0        2933.0      0.806001     69.0       239.47   \n",
       "4  24354     -1.0    -1.0          -1.0     -1.000000     -1.0        -1.00   \n",
       "\n",
       "   price_avgM1  price_maxM1  price_minM1   ...    地点263_max  地点263_min  \\\n",
       "0     4.827500         12.3         0.43   ...         -1.0       -1.0   \n",
       "1     6.080169         23.8         0.01   ...          5.6        3.6   \n",
       "2     2.841744         10.6         0.01   ...         -1.0       -1.0   \n",
       "3     3.470580         21.4        -1.14   ...         -1.0       -1.0   \n",
       "4    -1.000000         -1.0        -1.00   ...         -1.0       -1.0   \n",
       "\n",
       "   地点263_median  地点840_count  地点840_sum  地点840_avg  地点840_max  地点840_min  \\\n",
       "0          -1.0         -1.0       -1.0      -1.00       -1.0       -1.0   \n",
       "1           4.6         -1.0       -1.0      -1.00       -1.0       -1.0   \n",
       "2          -1.0         28.0      119.0       4.25        6.0        3.0   \n",
       "3          -1.0         -1.0       -1.0      -1.00       -1.0       -1.0   \n",
       "4          -1.0         -1.0       -1.0      -1.00       -1.0       -1.0   \n",
       "\n",
       "   地点840_median   label  \n",
       "0          -1.0  1000.0  \n",
       "1          -1.0     0.0  \n",
       "2           4.0  1000.0  \n",
       "3          -1.0     0.0  \n",
       "4          -1.0     0.0  \n",
       "\n",
       "[5 rows x 432 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       6530\n",
       "1000.0     515\n",
       "1500.0     335\n",
       "2000.0     239\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "to_train['label'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.text.Text at 0x7f085ea4ed10>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAEwCAYAAABCGI0AAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgA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AT4bTqn8c/AqyPib4BXS/oByST4wekI\nxKwwPtxktvdOAz4TEfcBfyBZs+etJPMVSOqKiG8APwM2AVeSnP00F2gD7omILuAZkqWlpwDXpovC\njQGukNTX0HdkVsVrN5mZWSYPZc3MLJNDwszMMjkkzMwsk0PCzMwyOSTMzCyTQ8LMzDI5JMzMLJND\nwszMMv03WzdMOom6kj8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f08612fccd0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "count_classes = pd.value_counts(to_train['label'], sort = True).sort_index()\n",
    "count_classes.plot(kind = 'bar')\n",
    "plt.title(\"Class histogram\")\n",
    "plt.xlabel(\"Class\")\n",
    "plt.ylabel(\"Frequency\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = to_train.ix[:, to_train.columns != 'label']\n",
    "y = to_train.ix[:, to_train.columns == 'label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def to_01(x):\n",
    "    if x != 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "y['01']=y['label'].apply(lambda x : to_01(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Number transactions train dataset: ', 3809)\n",
      "('Number transactions test dataset: ', 3810)\n",
      "('Total number of transactions: ', 7619)\n"
     ]
    }
   ],
   "source": [
    "# 这里的采样方式，会造成数据分布变化 // resample to all data\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# Whole dataset\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.5, random_state = 42)\n",
    "\n",
    "print(\"Number transactions train dataset: \", len(X_train))\n",
    "print(\"Number transactions test dataset: \", len(X_test))\n",
    "print(\"Total number of transactions: \", len(X_train)+len(X_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  from ipykernel import kernelapp as app\n"
     ]
    }
   ],
   "source": [
    "train=X_train\n",
    "train['label']=y_train['01']\n",
    "\n",
    "train.to_csv('./input/train_binary.csv',index=False)\n",
    "train=pd.read_csv('./input/train_binary.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Number of normal transactions: ', 539)\n",
      "('Number of fraud transactions: ', 539)\n",
      "('Total number of transactions in resampled data: ', 1078)\n"
     ]
    }
   ],
   "source": [
    "number_records_fraud = len(train[train.label != 0])\n",
    "fraud_indices = np.array(train[train.label != 0].index)\n",
    "\n",
    "# Picking the indices of the normal classes\n",
    "normal_indices = train[train.label == 0].index\n",
    "\n",
    "# Out of the indices we picked, randomly select \"x\" number (number_records_fraud)\n",
    "random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)\n",
    "random_normal_indices = np.array(random_normal_indices)\n",
    "\n",
    "# Appending the 2 indices\n",
    "under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])\n",
    "\n",
    "# Under sample dataset\n",
    "under_sample_data = train.iloc[under_sample_indices,:]\n",
    "\n",
    "X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'label']\n",
    "y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'label']\n",
    "\n",
    "# Showing ratio\n",
    "print(\"Number of normal transactions: \", len(under_sample_data[under_sample_data.label == 0]))\n",
    "print(\"Number of fraud transactions: \", len(under_sample_data[under_sample_data.label != 0]))\n",
    "print(\"Total number of transactions in resampled data: \", len(under_sample_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#nice_feature=pd.read_csv('input/nice_feature.csv',header=None,index_col=0)\n",
    "\n",
    "target = 'label'\n",
    "IDcol = 'id'\n",
    "ids = X_test['id'].values\n",
    "\n",
    "all_feature = [x for x in train.columns if x not in [target]]\n",
    "nice_feature=pd.read_csv('input/nice_feature.csv',header=None,index_col=0)\n",
    "feature_imp_place20=pd.read_csv('input/feature_imp_place20.csv')\n",
    "\n",
    "predictors = [ x for x in all_feature if (x in nice_feature.index)|(x in feature_imp_place20.feature.values)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.cross_validation import KFold, cross_val_score\n",
    "from sklearn.metrics import confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "三个想法：\n",
    "\n",
    "- 后threshold。多分类后再与threshold取交集。\n",
    "- 将二分类结果（概率）作为新特征\n",
    "- 先threshold。threshold改变数据分布后再多分类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "指标：二分类F1\n",
    "\n",
    "all-0: undersample: C = 0.1  threshold:0.54\n",
    "            \n",
    "1000-all:undersample: C = 1  threshold:0.48\n",
    "\n",
    "1500-all:undersample: C = 10  threshold:0.8\n",
    "\n",
    "2000-all:undersample: C = 1  threshold:0.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:11: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:15: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:23: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    }
   ],
   "source": [
    "best_c=0.1\n",
    "threshold=0.54\n",
    "predict_target='all'\n",
    "\n",
    "lr = LogisticRegression(C = best_c, penalty = 'l1')\n",
    "lr.fit(X_undersample[predictors],y_undersample.values.ravel())\n",
    "y_pred_undersample_proba = lr.predict_proba(X_test[predictors])\n",
    "\n",
    "y_pred_undersample_proba[:,1]\n",
    "\n",
    "X_test[predict_target]=y_pred_undersample_proba[:,1]\n",
    "\n",
    "y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\n",
    "\n",
    "X_test[predict_target]=y_test_predictions_high_recall\n",
    "\n",
    "def to_score(x):\n",
    "    if x ==True:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "X_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_plus=X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       3260\n",
       "1000.0     259\n",
       "1500.0     166\n",
       "2000.0     125\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_plus[target].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "test_plus[target]=y_test[target]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_plus=test_plus[test_plus['all']==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       852\n",
       "1000.0    195\n",
       "1500.0    129\n",
       "2000.0     97\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_plus[target].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model=XGBClassifier(max_depth=4,objective='multi:softmax',n_estimators=100,seed=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "validata=pd.read_csv('input/test_7r07part.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "best_c=0.1\n",
    "threshold=0.54\n",
    "predict_target='all'\n",
    "\n",
    "lr = LogisticRegression(C = best_c, penalty = 'l1')\n",
    "lr.fit(X_undersample[predictors],y_undersample.values.ravel())\n",
    "y_pred_undersample_proba = lr.predict_proba(validata[predictors])\n",
    "\n",
    "y_pred_undersample_proba[:,1]\n",
    "\n",
    "validata[predict_target]=y_pred_undersample_proba[:,1]\n",
    "\n",
    "y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\n",
    "\n",
    "validata[predict_target]=y_test_predictions_high_recall\n",
    "\n",
    "def to_score(x):\n",
    "    if x ==True:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "validata[predict_target]=validata[predict_target].apply(lambda x:to_score(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "validata_plus=validata[validata['all']==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       2795\n",
       "1000.0     226\n",
       "1500.0     130\n",
       "2000.0     115\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "validata[target].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       722\n",
       "1000.0    161\n",
       "1500.0    105\n",
       "2000.0     89\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "validata_plus[target].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/imblearn/base.py:306: UserWarning: The target type should be binary.\n",
      "  warnings.warn('The target type should be binary.')\n"
     ]
    }
   ],
   "source": [
    "oversampler=SMOTE(random_state=0)\n",
    "os_features,os_labels=oversampler.fit_sample(test_plus[predictors],test_plus[target])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/imblearn/base.py:306: UserWarning: The target type should be binary.\n",
      "  warnings.warn('The target type should be binary.')\n"
     ]
    }
   ],
   "source": [
    "os_features,os_labels=oversampler.fit_sample(os_features,os_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "os_labels_=pd.DataFrame(os_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2000.0    852\n",
       "1500.0    852\n",
       "1000.0    852\n",
       "0.0       852\n",
       "Name: 0, dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os_labels_[0].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1,\n",
       "       gamma=0, learning_rate=0.1, max_delta_step=0, max_depth=4,\n",
       "       min_child_weight=1, missing=None, n_estimators=100, nthread=-1,\n",
       "       objective='multi:softprob', reg_alpha=0, reg_lambda=1,\n",
       "       scale_pos_weight=1, seed=42, silent=True, subsample=1)"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(os_features,os_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predict_plus=model.predict(validata_plus[predictors])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  if __name__ == '__main__':\n"
     ]
    }
   ],
   "source": [
    "validata_plus['predict']=predict_plus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0       820\n",
       "1000.0    139\n",
       "1500.0     61\n",
       "2000.0     57\n",
       "Name: predict, dtype: int64"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "validata_plus['predict'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1077, 434)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "validata_plus.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3266, 433)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "validata.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "binary_result=validata.merge(validata_plus,on='id',how='outer').fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## Evaluation \n",
    "def f1_macro(label_truth, predictions):\n",
    "    df=pd.DataFrame(columns=[\"subsidy_x\",\"subsidy_y\"])\n",
    "    df.subsidy_y=predictions\n",
    "    df.subsidy_x=np.array(label_truth)\n",
    "    df.subsidy_y = df.subsidy_y.apply(lambda x:int(x))\n",
    "\n",
    "    \n",
    "    correct = df[df['subsidy_x'] == df['subsidy_y']]\n",
    "    s = 0\n",
    "    for i in [1000, 1500, 2000]:\n",
    "        print '\\n%d'%i\n",
    "        if sum(df['subsidy_x'] == i)!=0:\n",
    "            r = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_x'] == i)\n",
    "            print 'Recall---%s'%r\n",
    "        else: \n",
    "            r=0\n",
    "        if sum(df['subsidy_y'] == i)!=0:\n",
    "            p = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_y'] == i)        \n",
    "            print 'Precision---%s'%p\n",
    "        else:\n",
    "            p=0\n",
    "        if (r+p)!=0:\n",
    "            f = r*p*2/(r+p)\n",
    "            print 'F1---%s'%f\n",
    "        if not np.isnan(f):\n",
    "            s += (float(sum(df['subsidy_x'] == i))/df.shape[0])*f\n",
    "    \n",
    "    print '\\nF1-macro---%s'%s\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "1000\n",
      "Recall---0.119469026549\n",
      "Precision---0.194244604317\n",
      "F1---0.147945205479\n",
      "\n",
      "1500\n",
      "Recall---0.0846153846154\n",
      "Precision---0.180327868852\n",
      "F1---0.115183246073\n",
      "\n",
      "2000\n",
      "Recall---0.104347826087\n",
      "Precision---0.210526315789\n",
      "F1---0.139534883721\n",
      "\n",
      "F1-macro---0.0197354409234\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.01973544092339006"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f1_macro(binary_result['label_x'],binary_result['predict'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:11: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\ny_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\\n\\nX_test[predict_target]=y_test_predictions_high_recall\\n\\ndef to_score(x):\\n    if x ==True:\\n        return 1\\n    else:\\n        return 0\\n\\nX_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\\n\\n'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_c=1\n",
    "threshold=0.48\n",
    "predict_target='1000'\n",
    "\n",
    "lr = LogisticRegression(C = best_c, penalty = 'l1')\n",
    "lr.fit(X_undersample[predictors],y_undersample.values.ravel())\n",
    "y_pred_undersample_proba = lr.predict_proba(X_test[predictors])\n",
    "\n",
    "y_pred_undersample_proba[:,1]\n",
    "\n",
    "X_test[predict_target]=y_pred_undersample_proba[:,1]\n",
    "\n",
    "'''\n",
    "y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\n",
    "\n",
    "X_test[predict_target]=y_test_predictions_high_recall\n",
    "\n",
    "def to_score(x):\n",
    "    if x ==True:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "X_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:11: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\ny_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\\n\\nX_test[predict_target]=y_test_predictions_high_recall\\n\\ndef to_score(x):\\n    if x ==True:\\n        return 1\\n    else:\\n        return 0\\n\\nX_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\\n\\n'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_c=10\n",
    "threshold=0.8\n",
    "predict_target='1500'\n",
    "\n",
    "lr = LogisticRegression(C = best_c, penalty = 'l1')\n",
    "lr.fit(X_undersample[predictors],y_undersample.values.ravel())\n",
    "y_pred_undersample_proba = lr.predict_proba(X_test[predictors])\n",
    "\n",
    "y_pred_undersample_proba[:,1]\n",
    "\n",
    "X_test[predict_target]=y_pred_undersample_proba[:,1]\n",
    "\n",
    "'''\n",
    "y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\n",
    "\n",
    "X_test[predict_target]=y_test_predictions_high_recall\n",
    "\n",
    "def to_score(x):\n",
    "    if x ==True:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "X_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:11: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\ny_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\\n\\nX_test[predict_target]=y_test_predictions_high_recall\\n\\ndef to_score(x):\\n    if x ==True:\\n        return 1\\n    else:\\n        return 0\\n\\nX_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\\n\\n'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_c=1\n",
    "threshold=0.7\n",
    "predict_target='2000'\n",
    "\n",
    "lr = LogisticRegression(C = best_c, penalty = 'l1')\n",
    "lr.fit(X_undersample[predictors],y_undersample.values.ravel())\n",
    "y_pred_undersample_proba = lr.predict_proba(X_test[predictors])\n",
    "\n",
    "y_pred_undersample_proba[:,1]\n",
    "\n",
    "X_test[predict_target]=y_pred_undersample_proba[:,1]\n",
    "\n",
    "'''\n",
    "y_test_predictions_high_recall = y_pred_undersample_proba[:,1] > threshold\n",
    "\n",
    "X_test[predict_target]=y_test_predictions_high_recall\n",
    "\n",
    "def to_score(x):\n",
    "    if x ==True:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "X_test[predict_target]=X_test[predict_target].apply(lambda x:to_score(x))\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5443, 435)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X_test,y_test['label'],test_size = 0.5, random_state = 42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train=X_train\n",
    "train['label']=y_train\n",
    "\n",
    "test=X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Number of normal transactions: ', 393)\n",
      "('Number of fraud transactions: ', 393)\n",
      "('Total number of transactions in resampled data: ', 786)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# tips：\n",
    "# 尝试欠采样和smoth采样\n",
    "\n",
    "# undersample\n",
    "train.to_csv('input/train_half.csv',index=False)\n",
    "train=pd.read_csv('input/train_half.csv')\n",
    "\n",
    "number_records_fraud = len(train[train.label != 0])\n",
    "fraud_indices = np.array(train[train.label != 0].index)\n",
    "\n",
    "# Picking the indices of the normal classes\n",
    "normal_indices = train[train.label == 0].index\n",
    "\n",
    "# Out of the indices we picked, randomly select \"x\" number (number_records_fraud)\n",
    "random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)\n",
    "random_normal_indices = np.array(random_normal_indices)\n",
    "\n",
    "# Appending the 2 indices\n",
    "under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])\n",
    "\n",
    "# Under sample dataset\n",
    "under_sample_data = train.iloc[under_sample_indices,:]\n",
    "\n",
    "X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'label']\n",
    "y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'label']\n",
    "\n",
    "# Showing ratio\n",
    "print(\"Number of normal transactions: \", len(under_sample_data[under_sample_data.label == 0]))\n",
    "print(\"Number of fraud transactions: \", len(under_sample_data[under_sample_data.label != 0]))\n",
    "print(\"Total number of transactions in resampled data: \", len(under_sample_data))\n",
    "\n",
    "\n",
    "# Oversample\n",
    "Oversampling1000 = train.loc[train.label == 1000]\n",
    "Oversampling1500 = train.loc[train.label == 1500]\n",
    "Oversampling2000 = train.loc[train.label == 2000]\n",
    "'''\n",
    "for i in range(6):\n",
    "    train = train.append(Oversampling1000)\n",
    "for j in range(10):\n",
    "    train = train.append(Oversampling1500)\n",
    "for k in range(12):\n",
    "    train = train.append(Oversampling2000)\n",
    "'''\n",
    "for i in range(5):\n",
    "    train = train.append(Oversampling1000)\n",
    "for j in range(8):\n",
    "    train = train.append(Oversampling1500)\n",
    "for k in range(10):\n",
    "    train = train.append(Oversampling2000)\n",
    "    \n",
    "train = train[train['label'].notnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>college</th>\n",
       "      <th>rank</th>\n",
       "      <th>total_people</th>\n",
       "      <th>rank_percent</th>\n",
       "      <th>countM1</th>\n",
       "      <th>price_sumM1</th>\n",
       "      <th>price_avgM1</th>\n",
       "      <th>price_maxM1</th>\n",
       "      <th>price_minM1</th>\n",
       "      <th>...</th>\n",
       "      <th>地点840_sum</th>\n",
       "      <th>地点840_avg</th>\n",
       "      <th>地点840_max</th>\n",
       "      <th>地点840_min</th>\n",
       "      <th>地点840_median</th>\n",
       "      <th>all</th>\n",
       "      <th>1000</th>\n",
       "      <th>1500</th>\n",
       "      <th>2000</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30764</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.00</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.189398</td>\n",
       "      <td>0.253657</td>\n",
       "      <td>0.266131</td>\n",
       "      <td>0.253680</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17639</td>\n",
       "      <td>17.0</td>\n",
       "      <td>480.0</td>\n",
       "      <td>2051.0</td>\n",
       "      <td>0.234032</td>\n",
       "      <td>40.0</td>\n",
       "      <td>198.74</td>\n",
       "      <td>4.968500</td>\n",
       "      <td>31.4</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>...</td>\n",
       "      <td>196.5</td>\n",
       "      <td>4.180851</td>\n",
       "      <td>7.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.533066</td>\n",
       "      <td>0.532759</td>\n",
       "      <td>0.532071</td>\n",
       "      <td>0.532753</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4663</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1438.0</td>\n",
       "      <td>2304.0</td>\n",
       "      <td>0.624132</td>\n",
       "      <td>154.0</td>\n",
       "      <td>458.81</td>\n",
       "      <td>2.979286</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.01</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.783342</td>\n",
       "      <td>0.801125</td>\n",
       "      <td>0.804305</td>\n",
       "      <td>0.801130</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30657</td>\n",
       "      <td>18.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>193.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>70.0</td>\n",
       "      <td>256.78</td>\n",
       "      <td>3.668286</td>\n",
       "      <td>25.6</td>\n",
       "      <td>-3.18</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.092106</td>\n",
       "      <td>0.094557</td>\n",
       "      <td>0.093463</td>\n",
       "      <td>0.094513</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>22621</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>2830.0</td>\n",
       "      <td>0.694346</td>\n",
       "      <td>36.0</td>\n",
       "      <td>65.48</td>\n",
       "      <td>1.818889</td>\n",
       "      <td>25.0</td>\n",
       "      <td>0.01</td>\n",
       "      <td>...</td>\n",
       "      <td>8.4</td>\n",
       "      <td>8.400000</td>\n",
       "      <td>8.4</td>\n",
       "      <td>8.4</td>\n",
       "      <td>8.4</td>\n",
       "      <td>0.038584</td>\n",
       "      <td>0.041807</td>\n",
       "      <td>0.042328</td>\n",
       "      <td>0.041799</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 436 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id  college    rank  total_people  rank_percent  countM1  price_sumM1  \\\n",
       "0  30764     -1.0    -1.0          -1.0     -1.000000     -1.0        -1.00   \n",
       "1  17639     17.0   480.0        2051.0      0.234032     40.0       198.74   \n",
       "2   4663      3.0  1438.0        2304.0      0.624132    154.0       458.81   \n",
       "3  30657     18.0   193.0         193.0      1.000000     70.0       256.78   \n",
       "4  22621      8.0  1965.0        2830.0      0.694346     36.0        65.48   \n",
       "\n",
       "   price_avgM1  price_maxM1  price_minM1   ...    地点840_sum  地点840_avg  \\\n",
       "0    -1.000000         -1.0        -1.00   ...         -1.0  -1.000000   \n",
       "1     4.968500         31.4        -0.12   ...        196.5   4.180851   \n",
       "2     2.979286         12.0         0.01   ...          3.0   3.000000   \n",
       "3     3.668286         25.6        -3.18   ...         -1.0  -1.000000   \n",
       "4     1.818889         25.0         0.01   ...          8.4   8.400000   \n",
       "\n",
       "   地点840_max  地点840_min  地点840_median       all      1000      1500      2000  \\\n",
       "0       -1.0       -1.0          -1.0  0.189398  0.253657  0.266131  0.253680   \n",
       "1        7.0        1.0           4.0  0.533066  0.532759  0.532071  0.532753   \n",
       "2        3.0        3.0           3.0  0.783342  0.801125  0.804305  0.801130   \n",
       "3       -1.0       -1.0          -1.0  0.092106  0.094557  0.093463  0.094513   \n",
       "4        8.4        8.4           8.4  0.038584  0.041807  0.042328  0.041799   \n",
       "\n",
       "    label  \n",
       "0     0.0  \n",
       "1     0.0  \n",
       "2  1000.0  \n",
       "3     0.0  \n",
       "4     0.0  \n",
       "\n",
       "[5 rows x 436 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "target = 'label'\n",
    "IDcol = 'id'\n",
    "ids = test['id'].values\n",
    "\n",
    "all_feature = [x for x in train.columns if x not in [target]]\n",
    "#predictors = [ x for x in train.columns if (x in all_feature) & (x !='0all')]\n",
    "#predictors = [ x for x in all_feature if (x in all_feature)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predictors = [ x for x in all_feature if (x in nice_feature.index) | (x =='all')| (x =='1000')| (x =='1500')| (x =='2000')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['id',\n",
       " 'rank_percent',\n",
       " 'rest_sumM1',\n",
       " 'rest_minM1',\n",
       " 'price_sumM2',\n",
       " 'price_sumM3',\n",
       " 'rest_medianM3',\n",
       " 'price_minM7',\n",
       " 'rest_avgM8',\n",
       " 'price_sumM9',\n",
       " 'price_sumH7',\n",
       " 'price_medianH8',\n",
       " 'price_sumH9',\n",
       " 'rest_minH9',\n",
       " 'countH17',\n",
       " 'price_avgH17',\n",
       " 'price_maxH17',\n",
       " 'change_count',\n",
       " 'bathe_count',\n",
       " 'bathe_sum',\n",
       " 'all',\n",
       " '1000',\n",
       " '1500',\n",
       " '2000']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def f1_macro(label_truth, predictions):\n",
    "    df=pd.DataFrame(columns=[\"subsidy_x\",\"subsidy_y\"])\n",
    "    df.subsidy_y=predictions\n",
    "    df.subsidy_x=np.array(label_truth)\n",
    "    df.subsidy_y = df.subsidy_y.apply(lambda x:int(x))\n",
    "\n",
    "    \n",
    "    correct = df[df['subsidy_x'] == df['subsidy_y']]\n",
    "    s = 0\n",
    "    for i in [1000, 1500, 2000]:\n",
    "        #print i\n",
    "        r = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_x'] == i)\n",
    "        #print r\n",
    "        p = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_y'] == i)        \n",
    "        #print p\n",
    "        f = r*p*2/(r+p)\n",
    "        if not np.isnan(f):\n",
    "            s += (float(sum(df['subsidy_x'] == i))/df.shape[0])*f\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import xgboost as xgb\n",
    "model=xgb.XGBClassifier(seed=10,objective='multi:softmax')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.03048147230831718"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train[predictors],train[target])\n",
    "prediction=model.predict(X_test[predictors])\n",
    "f1_macro(y_test,prediction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.032220261329842625"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.029592512722876066#(原始特征)\n",
    "0.029187772192533705#(纯概率)\n",
    "0.030463249471639912#(阈值分类)\n",
    "0.032220261329842625#(阈值调整后)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feat_imp=pd.Series(model.booster().get_fscore()).sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_imp.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id                169\n",
       "countH17          138\n",
       "price_sumM3       130\n",
       "all               126\n",
       "price_sumH7       125\n",
       "rest_minH9        124\n",
       "price_maxH17      120\n",
       "rest_sumM1        119\n",
       "bathe_count       117\n",
       "price_sumH9       113\n",
       "price_avgH17      106\n",
       "1000              104\n",
       "rank_percent       95\n",
       "price_medianH8     90\n",
       "bathe_sum          90\n",
       "rest_minM1         83\n",
       "rest_medianM3      77\n",
       "price_minM7        72\n",
       "price_sumM2        69\n",
       "price_sumM9        66\n",
       "rest_avgM8         66\n",
       "1500               52\n",
       "change_count        7\n",
       "dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_imp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
